package com.zjb.datastructure.link;

import lombok.Data;

import java.util.Objects;

public class Link<T> {
    Node top = new Node();
    Node end = new Node();
    Node cur ;

    public Link() {
        top.next = end;
        cur = top;
    }

    private void add(T t) {

        Node node = new Node(t);
        node.next = end;
        cur.next = node;
        cur = node;

    }

    private void remove(T t) {
        Node node = new Node(t);
        Node cur = top;
        Node next = top.next;
        while (next != null) {
            if (Objects.equals(next.value, node.value)) {
                cur.next = next.next;
                break;
            }
            cur = next;
            next = next.next;
        }

    }

    private void reverse() {


        Node pre = top;
        Node cur = top.next;
        Node tmp ;
        while (cur != end) {
            tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }



    }


    @Data
    class Node {
        T value;
        Node next;

        public Node() {
        }

        public Node(T value) {
            this.value = value;
        }
    }

    public static void main(String[] args) {
        Link<Integer> link = new Link<>();
        link.add(1);
        link.add(2);
        link.add(3);
        link.add(4);
        System.out.println(link);
        link.reverse();
        System.out.println(link);

    }
}
